package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.educational.entity.AttendanceRecord;
import com.yuyou.fn.educational.entity.AttendanceRecordDetail;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.service.IAttendanceRecordService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.vo.AttendanceRecordDetailUpdateVo;
import com.yuyou.fn.educational.vo.CourseClassQueryVo;
import com.yuyou.fn.platform.entity.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by wens on 2017/4/16.
 */
@Controller
@RequestMapping("/api/admin/attendance")
public class AdminAttendanceController  extends AdminApiBaseController {

    @Resource
    private ICourseClassService courseClassService ;

    @Resource
    private IAttendanceRecordService attendanceRecordService ;


    @ApiOperation("考勤班级列表")
    @RequestMapping(value = "/courseClassList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<CourseClass>> courseClassList(CourseClassQueryVo courseClassQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {

        SysUser user = getUser(request);
        courseClassQueryVo.setAreaTeamId(user.getAreaTeamId());
        courseClassQueryVo.setBusTeamId(user.getBusTeamId());
        courseClassQueryVo.setStatus(1);
        courseClassQueryVo.setIsArrangeRoom(1);
        courseClassQueryVo.setIsArrangeTeacher(1);
        courseClassQueryVo.setIsArrangeTime(1);
        Page<CourseClass> page = courseClassService.findCourseClassByQueryForPage(courseClassQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("考勤列表")
    @RequestMapping(value = "/attendanceRecordList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<AttendanceRecord> > attendanceRecordList(@RequestParam("classId") Long classId, HttpServletRequest request) {
        List<AttendanceRecord> recordList = attendanceRecordService.findAttendanceRecordByClassId(classId);
        return ReturnResult.success(recordList);
    }

    @ApiOperation("考勤详情")
    @RequestMapping(value = "/attendanceRecordDetailList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<AttendanceRecordDetail> > attendanceRecordDetailList(@RequestParam("recordId") Long recordId, HttpServletRequest request) {
        List<AttendanceRecordDetail> recordDetailList = attendanceRecordService.findAttendanceRecordDetailByRecordId(recordId);
        return ReturnResult.success(recordDetailList);
    }

    @ApiOperation("保存考勤详情")
    @RequestMapping(value = "/saveAttendanceRecordDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveAttendanceRecordDetail(@RequestParam("recordId") Long recordId , @RequestParam("details") String detailsStr, HttpServletRequest request) {

        List<AttendanceRecordDetailUpdateVo> details = JSONObject.parseObject(detailsStr, new TypeReference<List<AttendanceRecordDetailUpdateVo>>() {
        });
        attendanceRecordService.updateAttendanceRecordDetail(recordId , details);
        return ReturnResult.success("OK");
    }



}
